﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Data.Linq.Mapping;
using System.Security.Principal;

namespace eCRAM.Model
{
    [Table(Name = "Users")]
    public class User : IPrincipal
    {
        public User()
        {
            CreationDate = DateTime.Now;
        }

        public User(IIdentity identity)
        {
            Identity = identity;
        }


        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public Guid Id { get; set; }

        [Column]
        public string UserName { get; set; }

        [Column]
        public string Password { get; set; }

        [Column]
        public string FirstName { get; set; }

        [Column]
        public string LastName { get; set; }

        [Column]
        public string Company { get; set; }

        [Column]
        public bool IsActive { get; set; }

        [Column]
        public DateTime CreationDate { get; internal set; }

        [Column]
        public UserRole Role { get; set; }

        [Column]
        public string Email { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public IIdentity Identity
        {
            get;
            private set;
        }

        /// <summary>
        /// Implement the base interface method
        /// It's heck the role of user
        /// </summary>
        /// <param name="userRole"></param>
        /// <returns></returns>
        public  bool IsInRole(string role) {
            
            bool isInRole = false;

            //convert enum to string using System.Enum.ToString()
            if (Role.ToString() == role)
            {
                isInRole = true;
            }

            return isInRole;    
        }

        //public void CreateUser() {

        //    UserRepository userRepo = new UserRepository();
        //    userRepo.Add(this);
        //    userRepo.SubmitChanges();
            
        //}


        //public void DeleteUser()
        //{
        //    UserRepository userRepo = new UserRepository();
        //    userRepo.Delete(this);
        //    userRepo.SubmitChanges();
        //}


        //public void UpdateUser(User user) {
        //    UserRepository userRepo = new UserRepository();
        //    userRepo.Update(user);
        //    userRepo.SubmitChanges();
        //}

    }
}